package com.person.service.impl;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.person.common.BaseContext;
import com.person.common.CustomException;
import com.person.dto.SetmealDto;
import com.person.mapper.SetmealDishMapper;
import com.person.mapper.SetmealMapper;
import com.person.pojo.PageBean;
import com.person.pojo.SetmealDish;
import com.person.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Override
    public void add(SetmealDto setmealDto) {

        setmealDto.setId(IdUtil.getSnowflakeNextId());
        setmealDto.setCreateTime(LocalDateTime.now());
        setmealDto.setUpdateTime(LocalDateTime.now());
        setmealDto.setCreateUser(BaseContext.getCurrentId());
        setmealDto.setUpdateUser(BaseContext.getCurrentId());
        log.info("添加套餐：{}",setmealDto);
        setmealMapper.add(setmealDto);
    }

    @Override
    public PageBean page(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page,pageSize);

        //按姓名条件查找
        List<SetmealDto> setmealDtoss = setmealMapper.inquire(name);

        //
        //给套餐分类 名称赋值
        for (SetmealDto setmealDto : setmealDtoss) {
            //获取dto对象中的categoryId，再查找category表中对应的id，返回分类name
            String categoryName = setmealMapper.selectCategory(setmealDto.getCategoryId());
            //将分类name 添加进dto对象中的categoryName中
            setmealDto.setCategoryName(categoryName);
        }

        Page<SetmealDto> p =(Page<SetmealDto>) setmealDtoss;
        PageBean pageBean=new PageBean(p.getTotal(),p.getResult());
        log.info("pagebean {}",pageBean);


        return pageBean;
    }

    @Override
    public SetmealDto selectSetmeal(Long id) {
        SetmealDto SetmealDto = setmealMapper.select(id);
        Long setmealId = SetmealDto.getId();

        List<SetmealDish> setmealDish = setmealDishMapper.select(setmealId);
        SetmealDto.setSetmealDishes(setmealDish);
        return SetmealDto;
    }

    @Override
    public void update(List<Long> ids) {
        setmealMapper.update(ids);
    }

    @Override
    public void updateStart(List<Long> ids) {

        setmealMapper.updateStartSell(ids);
    }

    @Override
    @Transactional
    public void delete(List<Long> ids) {
        //查询套餐状态
        int count = setmealMapper.selectStatus(ids);
        //count>0表示，状态是起售不能删除
        if (count>0){
            throw new CustomException("套餐为起售状态，无法删除");
        }
        //删除套餐
        setmealMapper.delete(ids);
        //删除套餐对应的口味
        setmealDishMapper.delete(ids);

    }

    @Override
    @Transactional
    public void save(SetmealDto setmealDto) {
        //设置默认属性
        setmealDto.setUpdateUser(BaseContext.getCurrentId());
        setmealDto.setUpdateTime(LocalDateTime.now());
        //修改套餐
        setmealMapper.updateSetmeal(setmealDto);
        //先删除套餐对应的菜品
        setmealDishMapper.remove(setmealDto.getId());

        //设置默认属性
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {

            setmealDish.setSetmealId(setmealDto.getId());
            setmealDish.setId(IdUtil.getSnowflakeNextId());
            setmealDish.setCreateTime(LocalDateTime.now());
            setmealDish.setCreateUser(BaseContext.getCurrentId());
            setmealDish.setUpdateTime(LocalDateTime.now());
            setmealDish.setUpdateUser(BaseContext.getCurrentId());
        }
        //重新添加
        setmealDishMapper.add(setmealDishes);
    }

    @Override
    public List<SetmealDto> list(Long categoryId) {
        List<SetmealDto> setmealDtos = setmealMapper.list(categoryId);
        for (SetmealDto setmealDto : setmealDtos) {
            setmealDto.setSetmealDishes(setmealDishMapper.select(setmealDto.getId()));
        }

        return setmealDtos;
    }


}
